package com.eclink.asset.consume.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eclink.asset.consume.domain.dao.ConsumeAuditConfigNode;
import com.eclink.asset.consume.mapper.ConsumeAuditConfigNodeMapper;
import com.eclink.asset.domain.AuditUser;
import com.eclink.asset.domain.AuditConfigNode;
import com.eclink.asset.mapper.AuditUserMapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 耗材审批节点Service业务层处理
 *
 *
 * @author zcc
 */
@Service
public class ConsumeAuditConfigNodeService {

    @Resource
    private ConsumeAuditConfigNodeMapper consumeAuditConfigNodeMapper;

    @Resource
    private AuditUserMapper auditUserMapper;

    /**
     * 查询
     *
     * @param id ID
     * @return 结果
     */
    public ConsumeAuditConfigNode selectById(Integer id) {
        return consumeAuditConfigNodeMapper.selectById(id);
    }



    /**
     * 查询用户
     *
     * @return 结果
     */
    public List<AuditUser> listUser() {
        QueryWrapper<AuditUser> qw = new QueryWrapper<>();
        return auditUserMapper.selectList(qw);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param configId 节点所属的审核流程ID
     * @return 【请填写功能名称】
     */
    public List<ConsumeAuditConfigNode> selectListAll(String configId) {

        MPJLambdaWrapper<ConsumeAuditConfigNode> wrapper = new MPJLambdaWrapper<ConsumeAuditConfigNode>()
                .selectAll(AuditConfigNode.class);
        wrapper.eq("config_id", configId);
        wrapper.orderByAsc(AuditConfigNode::getStep);
        List<ConsumeAuditConfigNode> nodeList = consumeAuditConfigNodeMapper.selectJoinList(ConsumeAuditConfigNode.class,wrapper);

        System.out.println(nodeList);

        return nodeList;
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param auditConfigNode 【请填写功能名称】
     * @return 结果
     */
    public int insert(ConsumeAuditConfigNode auditConfigNode) {
        auditConfigNode.setDelFlag(0);
        auditConfigNode.setCreateTime(LocalDateTime.now());
        return consumeAuditConfigNodeMapper.insert(auditConfigNode);
    }

    /**
     * 修改审批流程的节点
     *
     * @param list 要更新的节点列表
     */
    public void update(List<ConsumeAuditConfigNode> list) {

        for(ConsumeAuditConfigNode node : list) {
            if (node.getId()!=null && node.getId()!=0) {
                node.setUpdateTime(LocalDateTime.now());
                node.setUpdateUserId(SecurityUtils.getUserId());
                consumeAuditConfigNodeMapper.updateById(node);
            } else {
                node.setCreateTime(LocalDateTime.now());
                node.setUpdateTime(LocalDateTime.now());
                node.setCreateUserId(SecurityUtils.getUserId());
                node.setUpdateUserId(SecurityUtils.getUserId());
                consumeAuditConfigNodeMapper.insert(node);
            }
        }
    }

    /**
     * 删除节点信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    public int deleteById(Long id) {
        return consumeAuditConfigNodeMapper.deleteById(id);
    }
}
